﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using AdoNet = System.Data;

namespace DataSet4Com
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComDefaultInterface(typeof(IUniqueConstraint))]
    [Guid("1F32D5E0-B111-423A-950A-90A2F3608676")]
    public class UniqueConstraint : IUniqueConstraint, IConstraint
    {
        private AdoNet.UniqueConstraint _adoNetUniqueConstraint;

        public UniqueConstraint() : this(null) { }
                
        public UniqueConstraint(AdoNet.UniqueConstraint adoNetUniqueConstraint)
        {
            if (adoNetUniqueConstraint == null)
                throw new ArgumentNullException("adoNetUniqueConstraint");

            _adoNetUniqueConstraint = adoNetUniqueConstraint;
        }

        [ComVisible(false)]
        public AdoNet.UniqueConstraint AdoNetUniqueConstraint
        {
            get { return _adoNetUniqueConstraint; }
        }

        public virtual DataColumnList Columns
        {
            get
            {
                DataColumn[] result = (from column in _adoNetUniqueConstraint.Columns
                                       select new DataColumn(column)).ToArray();

                return new DataColumnList(result);
            }
        }
        
        public bool IsPrimaryKey 
        {
            get { return _adoNetUniqueConstraint.IsPrimaryKey; }
        }

        public DataTable Table
        {
            get
            {
                return new DataTable(_adoNetUniqueConstraint.Table);
            }
        }

        public bool Equals(IUniqueConstraint constraint)
        {
            return _adoNetUniqueConstraint.Equals(constraint);
        }

        public new int GetHashCode()
        {
            return _adoNetUniqueConstraint.GetHashCode();
        }

        public virtual string ConstraintName 
        {
            get { return _adoNetUniqueConstraint.ConstraintName; }
            set { _adoNetUniqueConstraint.ConstraintName = value; }
        }

        [ComVisible(false)]
        public AdoNet.Constraint AdoNetConstraint
        {
            get { return (AdoNet.Constraint)_adoNetUniqueConstraint; }
        }
    }

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("5EB50615-29DC-4C16-A126-F30748A5E6A7")]
    public interface IUniqueConstraint
    {
        DataColumnList Columns { get; }

        bool IsPrimaryKey { get; }

        DataTable Table { get; }

        bool Equals(IUniqueConstraint constraint);

        int GetHashCode();

        string ConstraintName { get; set; }
    }
}
